#include <object.h>

namespace losor
{
	object::object( string type, string caption, object *parent ):
		m_type( type ),
		m_caption( caption ),
		m_parent( parent )
	{
	}

	object::~object()
	{
		list<object*>::iterator i;
		for( i = m_child.begin(); i != m_child.end(); ++i )
			delete *i;
		for( i = m_ref.begin(); i != m_ref.end(); ++i )
			delete *i;
		for( i = m_beref.begin(); i != m_beref.end(); ++i )
			delete *i;
	}
	
	void object::add_child( object *child )
	{
		list<object*>::iterator i;
		for( i = m_child.begin(); i != m_child.end(); ++i )
		{
			if( *i == child )
				break;
		}
		if( i == m_child.end() )
			m_child.push_back( child );
	}
	
	void object::add_ref_obj( object* ref )
	{
		list<object*>::iterator i;
		for( i = m_ref.begin(); i != m_ref.end(); ++i )
		{
			if( *i == ref )
				break;
		}
		if( i == m_ref.end() )
			m_ref.push_back( ref );
	}
	
	void object::add_beref_obj( object *beref )
	{
		list<object*>::iterator i;
		for( i = m_beref.begin(); i != m_beref.end(); ++i )
		{
			if( *i == beref )
				break;
		}
		if( i == m_beref.end() )
			m_beref.push_back( beref );
	}
	
	void object::rm_child( object *child )
	{
		list<object*>::iterator i;
		for( i = m_child.begin(); i != m_child.end(); ++i )
		{
			if( *i == child )
			{
				delete *i;
				m_child.erase( i );
				break;
			}
		}
	}
	
	void object::rm_ref( object *ref )
	{
		list<object*>::iterator i;
		for( i = m_ref.begin(); i != m_ref.end(); ++i )
		{
			if( *i == ref )
			{
				m_ref.erase( i );
				break;
			}
		}
	}
	
	void object::rm_beref( object *beref )
	{
		list<object*>::iterator i;
		for( i = m_beref.begin(); i != m_beref.end(); ++i )
		{
			if( *i == beref )
			{
				m_beref.erase( i );
				break;
			}
		}
	}
	
	void object::reset( string type, string caption, object *parent )
	{
		m_type = type;
		m_caption = caption;
		m_parent = parent;
	}

	const list<object*>& object::get_child() const
	{
		return m_child;
	}
	
	const list<object*>& object::get_ref() const
	{
		return m_ref;
	}
	
	const list<object*>& object::get_beref() const
	{
		return m_beref;
	}
	
	string object::get_type() const
	{
		return m_type;
	}
	
	string object::get_caption() const
	{
		return m_caption;
	}
	
}

